import { http } from "@/utils/http";

// 抄表数据查询参数
export interface MeterReadingQueryParams {
  page: number;
  limit: number;
  name?: string;
}

// 抄表数据添加参数
export interface MeterReadingAddParams {
  cellId: number;
  buildingTypeId: number;
  buildingId: number;
  houseId: number;
  chargestandardId: number;
  currentMonth: number;
  startingDegree: number;
  endDegree: number;
  magnification: number;
}

// 抄表数据删除参数
export interface MeterReadingDeleteParams {
  ids: number[];
}

// 抄表数据列表项
export interface MeterReadingItem {
  id: number;
  cellName: string;
  buildingTypeName: string;
  buildingName: string;
  houseNum: string;
  chargestandardName: string;
  unitPrice: number;
  currentMonth: number;
  startingDegree: number;
  endDegree: number;
  magnification: number;
  // 前端计算的字段
  usage?: number;
}

// 分页响应数据
export interface MeterReadingPageResponse {
  list: MeterReadingItem[];
  total: number;
  pageNum: number;
  pageSize: number;
  size: number;
  pages: number;
}

// API响应包装
export interface ApiResponse<T> {
  code: number;
  data: T;
  message: string;
}

/**
 * 查询抄表数据列表
 */
export const getMeterReadingList = (params: MeterReadingQueryParams) => {
  return http.request<ApiResponse<MeterReadingPageResponse>>(
    "post",
    "/SpMeterReadingdataController/queryList",
    {
      data: params
    }
  );
};

/**
 * 添加抄表数据
 */
export const addMeterReading = (data: MeterReadingAddParams) => {
  return http.request("post", "/SpMeterReadingdataController/add", {
    data
  });
};

/**
 * 删除抄表数据
 */
export const deleteMeterReading = (data: MeterReadingDeleteParams) => {
  return http.request("delete", "/SpMeterReadingdataController/delete", {
    data
  });
};

/**
 * 导入抄表数据
 */
export const importMeterReading = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return http.request("post", "/SpMeterReadingdataController/import", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/**
 * 导出抄表数据
 */
export const exportMeterReading = () => {
  return http.request("get", "/SpMeterReadingdataController/export", {
    responseType: "blob"
  });
};
